import Vue from 'vue'
import VueRouter from 'vue-router'
import LayoutView from '../LayoutView.vue'
Vue.use(VueRouter)
// 配置路径
// 同一级路由： 设置隐藏底部tab栏目
// 嵌套路由：   显示tab栏目
const routes = [
  {
    path: '/',
    redirect: '/index', //打开页面就想显示首页
  },
  {
    path: '/',
    name: 'layout',
    component: LayoutView,
    children: [
       {
         path: 'index',
         name: 'home',
         component: () => import('../views/HomeView.vue'),
         meta: {
            // 通过路由meta对象添加布尔值，控制哪些路由要显示哪些标签
            isShowTab: true
         },
       },
       {
          path: 'list',
          name: 'list',
          component: () => import('../views/ListView.vue'),
          meta: {
            isShowTab: true
         },
        }
        ,
       {
          path: 'my',
          name: 'my',
          component: () => import('../views/MyView.vue'),
          meta: {
            isShowTab: true
         },
        }
        ,
        {
           path: 'category',
           name: 'category',
           component: () => import('../views/CategoryView.vue'),
           meta: {
             isShowTab: true
          },
         }
    ]
  },
  
  {
    path: '/detail',
    name: 'detail',
    component: () => import('../views/DetailView.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/playlist',
    name: 'playlist',
    component: () => import('../views/PlayList.vue')
  },
  {
    path: '/album',
    name: 'album',
    component: () => import('../views/AlbumView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/editinfo',
    name: 'editinfo',
    component: () => import('../views/EditInfo.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/RegisterView.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import('../views/ErrorView.vue')
  },
]

const router = new VueRouter({
  routes
})


// 导出路由实例对象
export default router;




// 由于vue-router最新版本3.5.x，引入了promise，push、replace方法会返回一个Promise。
// 当传递参数多次且重复，或是没有写成功或失败的回调。会抛出异常，因此出现上面现象
// 解决方案：重写$router的push和replace方法

//重写push|replace方法
//先把VueRouter的push和replace方法保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location, resolve, reject) {
    // 此函数上下文(this指向)为VueRouter的一个实例
    if (resolve && reject) {    
        //如果我们自己指定了成功/失败的回调，则自己传入
        originPush.call(this, location, resolve, reject)
        //若此时直接使用originPush()，相当于调用了函数originPush()，则函数内的this指向window（内部代码将无法执行）。故应用call或apply方法修改this指向
    } else {    //如果我们没有指定成功/失败的回调，则自动帮我们生成，防止报错
        originPush.call(this, location, () => { }, () => { })
    }
}
// replace方法同理
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}

